<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { userInfoService } from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import useTokenStore from '@/stores/token.js'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'

const matchedUser = ref({
    userAccount: '',
    userRoleDescriptive: ''
})

const userInfoStore = useUserInfoStore()
const tokenStore = useTokenStore()
//获取用户详细信息函数
const getUserInfo = async () => {
    // 获取当前登录用户的 userAccount
    const currentAccount = tokenStore.userAccount
    try {
        let result = await userInfoService()
        if (result.code === 200) {
            const user = result.data.userQueryAllVos.find(user => user.userAccount === currentAccount)
            if (user) {
                // 将用户信息存入 pinia 中
                userInfoStore.setInfo(user)
                // 更新 matchedUser
                matchedUser.value = user
            }
        } else {
            ElMessage.error('账号异常!')
        }

    } catch (error) {
        ElMessage.error('账号异常!')
        console.log(error)
    }

}
//调用 getUserInfo
getUserInfo()
//条目被点击后，调用的函数
const router = useRouter()
const handleCommand = (command) => {
    if (command === 'logout') {
        ElMessageBox.confirm(
            '您确认要退出登录吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //退出登录
                //清空pinia中存储的token及个人信息
                tokenStore.removeToken()
                tokenStore.removeUserAccount()
                userInfoStore.removeInfo()
                //退出后跳转至登录页面
                router.push('/login')

                ElMessage({
                    type: 'success',
                    message: '退出登录成功!',
                })

            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '用户取消了退出登录',
                })
            })
    } else {
        router.push('/userCenter/' + command)
    }
}
</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu :default-active="$route.path" active-text-color="#66ccff" background-color="#232323"
                text-color="#fff" router unique-opened>
                <!-- 使用router-link包裹主页菜单项 -->
                <router-link to="/index">
                    <el-menu-item index="/index">
                        <el-icon>
                            <img src="../assets/index.png" />
                        </el-icon>
                        <span>主页</span>
                    </el-menu-item>
                </router-link>

                <el-sub-menu index="/user">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>用户管理</span>
                    </template>
                    <el-menu-item index="/user/userManage">
                        <el-icon>
                            <img src="../assets/empolyee.png" />
                        </el-icon>
                        <span>职工管理</span>
                    </el-menu-item>
                    <el-menu-item index="/user/householderManage">
                        <el-icon>
                            <img src="../assets/householders.png" />
                        </el-icon>
                        <span>住户管理</span>
                    </el-menu-item>
                    <el-menu-item index="/user/personAccessRecords">
                        <el-icon>
                            <img src="../assets/householderRecords.png" />
                        </el-icon>
                        <span>人员出入记录</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-sub-menu index="/car">
                    <template #title>
                        <el-icon>
                            <img src="../assets/car.png" />
                        </el-icon>
                        <span>车辆管理</span>
                    </template>
                    <el-menu-item index="/car/carManage">
                        <el-icon>
                            <img src="../assets/licensePlate.png" />
                        </el-icon>
                        <span>车牌录入</span>
                    </el-menu-item>
                    <el-menu-item index="/car/carAccessRecordsVue">
                        <el-icon>
                            <img src="../assets/carRecords.png" />
                        </el-icon>
                        <span>车辆出入记录</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-sub-menu index="/personal">
                    <template #title>
                        <el-icon>
                            <img src="../assets/peresonCenter.png" />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/userCenter/resetPassword">
                        <el-icon>
                            <img src="../assets/pwd.png" />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div v-if="matchedUser.userAccount">
                    用户名：<strong>{{ matchedUser.userAccount }}</strong>
                </div>
                <div v-if="matchedUser.userRoleDescriptive">
                    身份：<strong>{{ matchedUser.userRoleDescriptive }}</strong>
                </div>
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>XX物业管理系统 ©2024 Created by 小江</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    img {
        width: 18px;
        height: 18px;
    }

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 100px;
            background: url('@/assets/logo_title.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>